package com.huijinhui.wlmtsys.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huijinhui.wlmtsys.enums.AppHttpCodeEnum;
import com.huijinhui.wlmtsys.mapper.NewsMapper;
import com.huijinhui.wlmtsys.mapper.ProjectsMapper;
import com.huijinhui.wlmtsys.pojo.dtos.PageDTO;
import com.huijinhui.wlmtsys.pojo.dtos.ResponseResult;
import com.huijinhui.wlmtsys.pojo.entity.NewsPageQuery;
import com.huijinhui.wlmtsys.pojo.entity.PageQuery;
import com.huijinhui.wlmtsys.pojo.entity.WlmtsysNews;
import com.huijinhui.wlmtsys.pojo.entity.WlmtsysProject;
import com.huijinhui.wlmtsys.service.FileStorageService;
import com.huijinhui.wlmtsys.service.NewsService;
import com.huijinhui.wlmtsys.service.ProjectsService;
import com.huijinhui.wlmtsys.utils.BeanUtils;
import com.huijinhui.wlmtsys.utils.CollUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ProjectsServiceImpl extends ServiceImpl<ProjectsMapper, WlmtsysProject> implements ProjectsService {

    @Autowired
    private ProjectsMapper projectsMapper;

    private final FileStorageService fileStorageService;

    /**
     * 获取project列表
     *
     * @return
     */
    @Override
    public List<WlmtsysProject> getProjectList() {
        List<WlmtsysProject> projectsList = list();
        return projectsList;
    }

    /**
     * 保存project
     *
     * @param project
     */
    @Override
    public void saveProject(WlmtsysProject project) {
        if (project != null && project.getContent() != null && project.getTitle() != null) {
            project.setCreateTime(LocalDateTime.now());
            save(project);
        }
    }

    /**
     * 删除project
     *
     * @param id
     */
    @Override
    public void deleteProject(Integer id) {
        if (id != null) {
            removeById(id);
        }
    }

    /**
     * 更新project
     *
     * @param project
     */
    @Override
    public void updateProject(WlmtsysProject project) {
        if (project != null && project.getId() != null) {
            project.setCreateTime(LocalDateTime.now());
            updateById(project);
        }
    }

    /**
     * 分页查询project
     *
     * @param query
     * @return
     */
    @Override
    public PageDTO<WlmtsysProject> queryProjectPage(PageQuery query) {
        Page<WlmtsysProject> page = this.lambdaQuery()
                .like(query.getTitle() != null, WlmtsysProject::getTitle, query.getTitle())
                .like(query.getContent() != null, WlmtsysProject::getContent, query.getContent())
                .orderByDesc(WlmtsysProject::getCreateTime)
                .page(query.toMpPage("date", false));
        List<WlmtsysProject> records = page.getRecords();
        if (CollUtils.isEmpty(records)) {
            return PageDTO.empty(page);
        }
        List<WlmtsysProject> wlmtsysProjects = new ArrayList<>();
        for (WlmtsysProject news : records) {
            WlmtsysProject wlmtsysProject = BeanUtils.copyBean(news, WlmtsysProject.class);
            wlmtsysProjects.add(wlmtsysProject);
        }
        return PageDTO.of(page, wlmtsysProjects);
    }

    /**
     * 上传图片
     *
     * @param multipartFile
     * @param projectId
     */
    @Override
    public ResponseResult uploadNews(MultipartFile multipartFile, Integer projectId) {
        //1.检查参数
        if(multipartFile == null || multipartFile.getSize() == 0 || projectId == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.上传图片到minIO中
        String fileName = UUID.randomUUID().toString().replace("-", "");
        //aa.jpg
        String originalFilename = multipartFile.getOriginalFilename();
        String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileId = null;
        try {
            fileId = fileStorageService.uploadImgFile("", fileName + postfix, multipartFile.getInputStream());
            log.info("上传图片到MinIO中，fileId:{}",fileId);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("WmMaterialServiceImpl-上传文件失败");
        }

        //3.更新数据库
        this.lambdaUpdate().eq(WlmtsysProject::getId,projectId).set(WlmtsysProject::getImage,fileId).update();
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    /**
     * 获取project详情
     *
     * @param id
     * @return
     */
    @Override
    public WlmtsysProject getProject(Integer id) {
        if (id == null) {
            throw new IllegalArgumentException("id不能为空");
        }
        return getById(id);
    }
}




